home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 20
/
Cream of the Crop 20 (Terry Blount) (1996).iso
/
program
/
ddj0796.zip
/
cprog.796
< prev
next >
Wrap
Text File
|
1996-05-22
|
11KB
|
362 lines
_C PROGRAMMING COLUMN_
by Al Stevens
Listing One
// --- setup.h
#include "resource.h"
class CSetupApp : public CWinApp
{
CString m_strSourcePath; // source path for install
public:
CSetupApp() { }
const CString& GetSourcePath() const
{ return m_strSourcePath; }
virtual BOOL InitInstance();
};
extern CSetupApp theApp;
Listing Two
// setup.cpp
#include "stdafx.h"
#include <io.h>
#include "setup.h"
#include "setupDlg.h"
CSetupApp theApp;
BOOL CSetupApp::InitInstance()
{
Enable3dControlsStatic();
BOOL bPathOK = FALSE;
// --- make sure they are installing from the root path of CD-ROM
m_strSourcePath = m_pszHelpFilePath;
int nIndex = m_strSourcePath.ReverseFind('\\');
if (nIndex > 0)
m_strSourcePath = m_strSourcePath.Left(nIndex+1);
if (m_strSourcePath.GetLength() == 3) {
if (m_strSourcePath.Right(2) == ":\\") {
UINT nDriveType = GetDriveType(m_strSourcePath);
if (nDriveType == DRIVE_CDROM)
bPathOK = TRUE;
}
}
if (!bPathOK) {
AfxMessageBox("Install from CD-ROM only", MB_ICONSTOP);
return FALSE;
}
int len = 100;
int dirlen;
char* psysdir = 0;
do {
dirlen = len;
delete psysdir;
psysdir = new char[dirlen+11];
int len = GetSystemDirectory(psysdir, dirlen);
} while (dirlen != len);
char* pnewdll = new char[dirlen+11];
strcpy(pnewdll, psysdir);
strcat(psysdir, "\\cygwin.dll");
strcat(pnewdll, "\\cygwin.sav");
if (_access(psysdir, 0) == 0) {
CString msg(
"Setup has found a file named cygwin.dll"
" in the system directory.\n\n("
);
msg += CString(psysdir);
msg += ")\n\n";
msg +=
"This DLL can, and probably will, cause Quincy to malfunction.\n"
"Different versions of this DLL are incompatible with one another.\n"
"You must rename or remove this file before you run the tutorial.\n"
"Setup can rename the file to cygwin.sav if you wish.\n\n"
"Should Setup rename the file now?";
if (AfxMessageBox(msg, MB_YESNO | MB_ICONQUESTION) == IDYES)
rename(psysdir, pnewdll);
}
delete [] pnewdll;
delete [] psysdir;
CSetupDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
return FALSE;
}
Listing Three
// setupDlg.h
const LONG nDiskSpaceRequired = 40000; // space required in KB
const INT nFileCount = 1000; // number of files to copy
class CSetupDlg : public CDialog
{
static UINT StartCopies(void*);
void CopyFiles();
void CopyFiles(const CString& strDestination, const CString& strSource);
BOOL m_bCopying;
BOOL m_bAborting;
public:
CSetupDlg(CWnd* pParent = NULL);
//{{AFX_DATA(CSetupDlg)
enum { IDD = IDD_SETUP_DIALOG };
CButton m_butInstall;
CProgressCtrl m_ctlProgress;
CString m_strDoing;
CString m_strPath;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
virtual LRESULT WindowProc(UINT message,WPARAM wParam,LPARAM Param);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnInstall();
virtual void OnCancel();
DECLARE_MESSAGE_MAP()
};
Listing Four
// --- setupDlg.cpp
#include "stdafx.h"
#include <direct.h>
#include <io.h>
#include <sys\stat.h>
#include "setup.h"
#include "setupDlg.h"
CSetupDlg::CSetupDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSetupDlg::IDD, pParent)
{
m_strDoing = _T("");
m_strPath = _T("");
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bCopying = FALSE;
m_bAborting = FALSE;
}
void CSetupDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_INSTALL, m_butInstall);
DDX_Control(pDX, IDC_PROGRESS, m_ctlProgress);
DDX_Text(pDX, IDC_DOING, m_strDoing);
DDX_Text(pDX, IDC_PATH, m_strPath);
}
BEGIN_MESSAGE_MAP(CSetupDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_INSTALL, OnInstall)
END_MESSAGE_MAP()
BOOL CSetupDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_strPath = "C:\\DDJTUTOR";
UpdateData(FALSE);
return TRUE;
}
void CSetupDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
CDialog::OnPaint();
}
HCURSOR CSetupDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSetupDlg::OnInstall()
{
UpdateData(TRUE);
// ----- edit path information
char drive = toupper(m_strPath.GetAt(0));
char path[] = "c:\\";
path[0] = drive;
if (drive < 'A' || drive > 'Z' ||
m_strPath[1] != ':' || m_strPath[2] != '\\') {
AfxMessageBox(
"Specify target drive and directory (e.g. C:\\DDJTUTOR)", MB_ICONSTOP);
return;
}
UINT nDriveType = GetDriveType(path);
if (nDriveType != DRIVE_REMOVABLE && nDriveType != DRIVE_FIXED) {
AfxMessageBox("Install to local hard disk only", MB_ICONSTOP);
return;
}
// ----- test for available disk space
m_strDoing = "Checking for available disk space";
UpdateData(FALSE);
DWORD nSectorsPerCluster;
DWORD nBytesPerSector;
DWORD nNumberOfFreeClusters;
DWORD nTotalNumberOfClusters;
GetDiskFreeSpace(path, &nSectorsPerCluster, &nBytesPerSector,
&nNumberOfFreeClusters, &nTotalNumberOfClusters);
LONG space =
(nNumberOfFreeClusters * (nSectorsPerCluster * nBytesPerSector)) / 1024;
if (space < nDiskSpaceRequired) {
CString msg;
msg.Format(
"Tutorial requires %ldKB\n"
"%c: has only %ldKB", nDiskSpaceRequired, *path, space);
AfxMessageBox(msg, MB_ICONSTOP);
m_strDoing = "";
UpdateData(FALSE);
return;
}
// ---- enough space, verify installation request
CString strMsg("Install to ");
strMsg += m_strPath + "?";
if (AfxMessageBox(strMsg, MB_YESNO | MB_ICONQUESTION) != IDYES){
m_strDoing = "";
UpdateData(FALSE);
return;
}
// ---- if path ends with \ delete it
if (m_strPath.Right(1) == "\\") {
if (m_strPath.Right(2) == ":\\") {
// ---- discourage installation to the root directory
if (AfxMessageBox(
"Do you really want to install into the root directory?",
MB_ICONQUESTION) == IDNO)
return;
}
else
m_strPath = m_strPath.Left(m_strPath.GetLength()-1);
}
// ----- test to see if installation subdirectory exists
if (_chdir(m_strPath) == 0) {
CString msg(m_strPath);
msg += " already exists.\nUse it?";
if (AfxMessageBox(msg, MB_YESNO | MB_ICONQUESTION) != IDYES) {
m_strDoing = "";
UpdateData(FALSE);
return;
}
}
m_butInstall.EnableWindow(FALSE);
m_ctlProgress.SetRange(0, nFileCount);
m_ctlProgress.SetStep(1);
m_bCopying = TRUE;
m_strDoing = "Copying:";
UpdateData(FALSE);
AfxBeginThread(&CSetupDlg::StartCopies, this);
}
UINT CSetupDlg::StartCopies(void* This)
{
((CSetupDlg*)This)->CopyFiles();
return 0;
}
void CSetupDlg::CopyFiles()
{
CopyFiles(m_strPath, theApp.GetSourcePath() + "DDJTUTOR");
if (m_bCopying)
AfxMessageBox("Setup completed");
else
AfxMessageBox(
"Note: Setup did not run to completion.\n"